use HTTP::Request::Common;
require LWP::UserAgent;
use IO::Socket::INET;
#use Socket;
use Sys::Hostname;

$LOCAL_IP_ADDRESS = null;


sub get_local_ip_address
{
	#only populate this the first time, then cache it. 
	if (null == $LOCAL_IP_ADDRESS)
	{ 
		my $sock = IO::Socket::INET->new(
		                       PeerAddr=> "example.com",
		                       PeerPort=> 80,
		                       Proto   => "tcp");
		$LOCAL_IP_ADDRESS = $sock->sockhost;
	}
	return $LOCAL_IP_ADDRESS;
}

sub get_local_host_name
{
	return hostname();
	
}

print get_local_ip_address() . "\n";
print hostname() . "\n";

#print get_local_host_name() + "\n";

sub get_site_to_generate()
{
	
	my $ua = LWP::UserAgent->new;
	$ua->timeout(10);
	my $response = $ua->request(POST 'http://0.0.0.0:3000/generations.xml',
								[ generator_ip   => get_local_ip_address,
								  generator_name => get_local_host_name ]);

	if ($response->is_success) {
	    print $response->decoded_content;  # or whatever
	}
	else {
	    die $response->status_line;
	}								
								
								
	
}

#sub get_product_id

get_site_to_generate();

=pod

while (@adrs=(gethostent())[4]) {
    for my $value (@adrs) {
        print join '.',unpack('C4',$value);
        print "\n";
    }
}



print $ENV{SERVER_NAME};
exit;

my $ua = LWP::UserAgent->new;
$ua->timeout(10);

my $response = $ua->request(GET 'http://0.0.0.0:3000/projects.xml/');

if ($response->is_success) {
    print $response->decoded_content;  # or whatever
}
else {
    die $response->status_line;
}



sub get_site_to_generate()
{
	my $response = $ua->request(POST 'http://0.0.0.0:3000/generations.xml/',
								[ generator_ip   => $my_ip,
								         email  => 'gisle@aas.no',
								         gender => 'M',
								         born   => '1964',
								         perc   => '3%',
								       ]);
	
}

=cut

#$ua->request(POST 'http://somewhere/foo', [foo => bar, bar => foo]);





=pod
use Net::Stomp;    

print "starting test \n";
my $stomp = Net::Stomp->new(
     { hostname =>'127.0.0.1',
      port => 61613} );
print "1 \n";

$stomp->connect();
print "2 \n";
$stomp->send(
      { 
        destination =>'/queue/enotify/test', 
        body =>'hello world. lets get it on again'
      } 
 );

print "3 \n";



$stomp->subscribe(
  {'destination'=>'/queue/enotify/testg', 
   'ack'=>'client','activemq.prefetchSize'=>1});
 
while(1)
{
  eval 
  { 
   if($stomp->can_read){ 
    my $message_body = $stomp->receive_frame->body;     
   if ($@)
    { 
       print "Error reading message body\n";      
       next;
     } 
     print "Message Recieved:" + $body; 
       eval {$stomp->ack({ 'frame' => $frame})}; 
       if ($@) { 
        print "Error acking message\n";  
     } 
   } 
 }; 
 if ($@) 
 {
    print "Error on eval";
 } 
 sleep 2000;
}
=cut